package com.tsd.system.service.impl;

import com.tsd.common.vo.BatchDeleteLogicVo;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.dao.SysUserClientMapper;
import com.tsd.system.entity.SysUser;
import com.tsd.system.entity.SysUserClient;
import com.tsd.system.entity.SysUserClientExt;
import com.tsd.system.entity.SysUserExt;
import com.tsd.system.service.SysUserClientService;
import com.tsd.core.thread.ThreadPoolTaskUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysUserClient 服务实现层
 *
 * @author zsy
 * @date 2021/01/15 10:01:49
 */
@Service
public class SysUserClientServiceImpl extends BaseServiceImpl implements SysUserClientService {

    @Resource
    private SysUserClientMapper sysUserClientMapper;

    @Override
    public SysUserClient getSysUserClientById(Long id) throws Exception {
        return sysUserClientMapper.selectByPrimaryKey(id);
    }

    @Override
    public SysUserClientExt queryBySid(String sid) throws Exception {
        return sysUserClientMapper.selectBySid(sid);
    }

    @Override
    public void deleteSysUserClient(Long id, SysUser opUser) throws Exception {
        SysUserClient old = sysUserClientMapper.selectByPrimaryKey(id);
        super.checkEmpty(old, "该记录不存在");
        super.autoInjectBaseData(old, opUser, this.TYPE_DELETE);
        sysUserClientMapper.updateByPrimaryKeySelective(old);
    }

    @Override
    public void deleteByClientList(List<String> clientList, SysUser opUser) throws Exception {
        BatchDeleteLogicVo logicVo = new BatchDeleteLogicVo(opUser);
        logicVo.setSidList(clientList);
        sysUserClientMapper.deleteLogicByClientList(logicVo);
    }

    @Override
    public void deleteysUserClientPhy(Long id) throws Exception {
        sysUserClientMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveSysUserClient(SysUserClient record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            sysUserClientMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            sysUserClientMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public List<SysUserClientExt> findSysUserClients(Map<String, Object> params, PageBean page) throws Exception {
        return sysUserClientMapper.selectByPage(params, page);
    }

    @Override
    public List<SysUserClientExt> findSysUserClients(Map<String, Object> params) throws Exception {
        return sysUserClientMapper.selectByParams(params);
    }

    @Override
    public void saveSysClientList2User(SysUserExt record, SysUser opUser) throws Exception {
        List<String> clientList = record.getClientList();
        List<SysUserClientExt> oldList = findSysUserClientsByUserSid(record.getSid());
        List<Long> delIds = ListUtil.map(oldList, SysUserClientExt::getId);
        if (!HlpUtils.isEmptyList(delIds)) {
            BatchDeleteLogicVo logicVo = new BatchDeleteLogicVo();
            logicVo.setIdList(delIds);
            sysUserClientMapper.deleteByIdList(logicVo);
        }
        if (!HlpUtils.isEmptyList(clientList)) {
            List<SysUserClientExt> insertList = new ArrayList<>();
            for (String id : clientList) {
                SysUserClientExt clientExt = new SysUserClientExt();
                clientExt.setClient_sid(id);
                clientExt.setUser_sid(record.getSid());
                super.autoInjectBaseData(clientExt, opUser, this.TYPE_CREATE);
                insertList.add(clientExt);
                if (insertList.size() == 100) {
                    sysUserClientMapper.insertList(insertList);
                    insertList.clear();
                }
            }
            if (!HlpUtils.isEmptyList(insertList)) {
                sysUserClientMapper.insertList(insertList);
            }
        }
    }

    @Override
    public List<SysUserClientExt> findSysUserClientsByUserSid(String userSid) {
        return sysUserClientMapper.selectByUserSid(userSid);
    }

    @Override
    public void settingClientSysUser(String clientSid, List<String> sysUserSidList, SysUser opUser) throws Exception {
        // 查询已存在的客户权限
        // 覆盖客户权限用户
        sysUserClientMapper.deleteByClientSid(clientSid);
        for (String sysUserSid : sysUserSidList) {
            SysUserClient t = new SysUserClient();
            t.setClient_sid(clientSid);
            t.setUser_sid(sysUserSid);
            saveSysUserClient(t, opUser);
        }
    }

    @Override
    public void settingClientSysUserNew(String clientSid, List<SysUserClientExt> list, SysUser opUser) throws Exception {
        SysUserClientExt params = new SysUserClientExt();
        params.setDr(0);
        params.setClient_sid(clientSid);
        List<SysUserClientExt> oldList = sysUserClientMapper.selectByEntity(params);

        List<String> submitList = ListUtil.map(list, SysUserClientExt::getUser_sid);
        List<SysUserClientExt> delList = ListUtil.filter(oldList, t -> !submitList.contains(t.getUser_sid()));
        if (!HlpUtils.isEmptyList(delList)) {
            List<Long> delIdList = ListUtil.map(delList, SysUserClientExt::getId);
            BatchDeleteLogicVo logicVo = new BatchDeleteLogicVo(opUser);
            logicVo.setIdList(delIdList);
            sysUserClientMapper.deleteLogicByIdList(logicVo);
        }
        List<SysUserClientExt> insertList = ListUtil.filter(list, t -> HlpUtils.isEmpty(t.getId()));
        if (!HlpUtils.isEmptyList(insertList)) {
            for (SysUserClientExt clientExt : insertList) {
                super.autoInjectBaseData(clientExt, opUser, this.TYPE_CREATE);
            }
            ThreadPoolTaskUtil.executeInList4Split(insertList, false, itemList -> sysUserClientMapper.insertList(itemList));
        }
        List<SysUserClientExt> updateList = ListUtil.filter(list, t -> HlpUtils.isEmpty(t.getId()));
        if (!HlpUtils.isEmptyList(updateList)) {
            for (SysUserClientExt clientExt : updateList) {
                super.autoInjectBaseData(clientExt, opUser, this.TYPE_UPDATE);
            }
            ThreadPoolTaskUtil.executeInList4Split(updateList, false, itemList -> sysUserClientMapper.updateList(itemList));
        }
    }
}
